/*
* @(#)StepInformationWindow.java
*
* Copyright (c) 2004 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.workflow.applet;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;

import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.bean.UserRole;
import com.dcivision.workflow.applet.net.Utility;
import com.dcivision.workflow.bean.MtmWorkflowStepUserActor;



/**
    StepInformationWindow.java

    @author          Beyond
    @company         DCIVision Limited
    @creation date   27/08/2004
    @version         $Revision: 1.2 $
    */

public class InterruptOwner extends JDialog {
  WorkflowRecordInfoWindow workflowRecordInfoWindow = null;
  JScrollPane jScrollPane1 = new JScrollPane();
  JScrollPane jScrollPane2 = new JScrollPane();
  JScrollPane jScrollPane3 = new JScrollPane();
  JScrollPane jScrollPane4 = new JScrollPane();
  JList userList = new JList();
  JList groupList = new JList();
  JList roleList = new JList();
  JList jListSpecialActor = new JList();
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  JLabel jLabel5 = new JLabel();
  JButton jButton1 = new JButton();

  public InterruptOwner() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  public InterruptOwner(WorkflowRecordInfoWindow workflowRecordInfoWindow, String title, boolean modal) {
    super(workflowRecordInfoWindow, title, modal);
    try {
      this.workflowRecordInfoWindow = workflowRecordInfoWindow;
      jbInit();
      pack();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }
  private void jbInit() throws Exception {
    this.getContentPane().setLayout(null);
    jScrollPane1.setBounds(new Rectangle(17, 65, 148, 125));
    jScrollPane2.setBounds(new Rectangle(175, 65, 148, 125));
    jScrollPane3.setBounds(new Rectangle(335, 66, 148, 125));
    jScrollPane4.setBounds(new Rectangle(490, 66, 159, 125));
    jLabel1.setText(AppletResource.lableArr[95]);
    jLabel1.setBounds(new Rectangle(23, 41, 104, 21));
    jLabel2.setText(AppletResource.lableArr[96]);
    jLabel2.setBounds(new Rectangle(186, 41, 104, 21));
    jLabel3.setText(AppletResource.lableArr[97]);
    jLabel3.setBounds(new Rectangle(347, 41, 104, 21));
    jLabel4.setText(AppletResource.lableArr[150]);
    jLabel4.setBounds(new Rectangle(495, 41, 104, 21));
    jLabel5.setText(AppletResource.lableArr[250]);
    jLabel5.setBounds(new Rectangle(21, 12, 363, 21));
    jButton1.setBounds(new Rectangle(271, 241, 104, 26));
    jButton1.setText(AppletResource.lableArr[24]);
    jButton1.addActionListener(new InterruptOwner_jButton1_actionAdapter(this));
    this.getContentPane().add(jScrollPane2, null);
    jScrollPane2.getViewport().add(groupList, null);
    this.getContentPane().add(jScrollPane1, null);
    jScrollPane1.getViewport().add(userList, null);
    this.getContentPane().add(jScrollPane3, null);
    jScrollPane3.getViewport().add(roleList, null);
    this.getContentPane().add(jScrollPane4, null);
    this.getContentPane().add(jLabel1, null);
    this.getContentPane().add(jLabel2, null);
    this.getContentPane().add(jLabel3, null);
    this.getContentPane().add(jLabel4, null);
    this.getContentPane().add(jLabel5, null);
    this.getContentPane().add(jButton1, null);
    jScrollPane4.getViewport().add(jListSpecialActor, null);
    initList();
  }

  public void initList(){
    ObjectInputStream inputFromServlet = null;
    List userRecordList = new ArrayList();
    List userGroupList = new ArrayList();
    List userRoleList = new ArrayList();
    String[][] specialActor ;
    List formSectionList = new ArrayList();
    List stepList = new ArrayList();
    List autoTaskTypeList = new ArrayList();

    String getDataURL  = "/workflow/appletMiddleware?dataType=GET_USERINFO"+"&workflowRecordID="+workflowRecordInfoWindow.workflowRecord.getID().toString();
    try{
      inputFromServlet = workflowRecordInfoWindow.httpOperation.getObjectInputStreamFromServlet(getDataURL);

      if(inputFromServlet == null){
        return;
      }
      Object obj = null;
      //obj = inputFromServlet.readObject();
    //  get bind list for select
      obj = inputFromServlet.readObject();
      if (obj instanceof List) {
        userRecordList = (List) obj;
      } else if (obj instanceof Exception) {
        //otherwise type cast it to Exception and throw that exception.
      throw (Exception) obj;
      }
      obj = inputFromServlet.readObject();
      if (obj instanceof List) {
        userGroupList = (List) obj;
      } else if (obj instanceof Exception) {
        //otherwise type cast it to Exception and throw that exception.
      throw (Exception) obj;
      }
      obj = inputFromServlet.readObject();
      if (obj instanceof List) {
        userRoleList = (List) obj;
      } else if (obj instanceof Exception) {
        //otherwise type cast it to Exception and throw that exception.
      throw (Exception) obj;
      }
      obj = inputFromServlet.readObject();
      specialActor = (String[][]) obj;

      obj = inputFromServlet.readObject();
      if (obj instanceof List) {
        formSectionList = (List) obj;
      } else if (obj instanceof Exception) {
        //otherwise type cast it to Exception and throw that exception.
      throw (Exception) obj;
      }
      obj = inputFromServlet.readObject();
      if (obj instanceof List) {
        stepList = (List) obj;
      } else if (obj instanceof Exception) {
        //otherwise type cast it to Exception and throw that exception.
      throw (Exception) obj;
      }

      obj = inputFromServlet.readObject();
      if (obj instanceof List) {
        autoTaskTypeList = (List) obj;
      } else if (obj instanceof Exception) {
        //otherwise type cast it to Exception and throw that exception.
        throw (Exception) obj;
      }

      DefaultListModel listUserModel = new DefaultListModel();
      DefaultListModel listGroupModel = new DefaultListModel();
      DefaultListModel listRoleModel = new DefaultListModel();
      DefaultListModel listSpecialModel = new DefaultListModel();
      DefaultListModel listFormSectionEModel = new DefaultListModel();
      DefaultListModel listFormSectionRModel = new DefaultListModel();
      DefaultListModel listStepModel = new DefaultListModel();
      for(int i=0; i<userRecordList.size() ; i++) {
        UserRecord userRecord = (UserRecord)userRecordList.get(i);
        Integer objID = null;
        String objText = null;
        //Method getIDMethod = Class.forName(className).getMethod("getID", null);
        //Method getTextMethod = Class.forName(className).getMethod(methodName, null);
        objID = userRecord.getID();
        objText = userRecord.getFullName();
        ListObject workflowUserInfo = new ListObject();
        workflowUserInfo.setID(objID.toString());
        workflowUserInfo.setName(objText);
        listUserModel.addElement(workflowUserInfo);
      }
      userList.setModel(listUserModel);
      for(int i=0; i<userGroupList.size() ; i++) {
        UserGroup userGroup = (UserGroup)userGroupList.get(i);
        Integer objID = null;
        String objText = null;
        //Method getIDMethod = Class.forName(className).getMethod("getID", null);
        //Method getTextMethod = Class.forName(className).getMethod(methodName, null);
        objID = userGroup.getID();
        objText = userGroup.getGroupName();
        ListObject workflowUserInfo = new ListObject();
        workflowUserInfo.setID(objID.toString());
        workflowUserInfo.setName(objText);
        listGroupModel.addElement(workflowUserInfo);

      }
      groupList.setModel(listGroupModel);
      for(int i=0; i<userRoleList.size() ; i++) {
        UserRole userRole = (UserRole)userRoleList.get(i);
        Integer objID = null;
        String objText = null;
        objID = userRole.getID();
        objText = userRole.getRoleName();
        ListObject workflowUserInfo = new ListObject();
        workflowUserInfo.setID(objID.toString());
        workflowUserInfo.setName(objText);
        listRoleModel.addElement(workflowUserInfo);
      }
      roleList.setModel(listRoleModel);

      for(int i=0; i<specialActor.length ; i++) {
        String objID = null;
        String objText = null;
        objID = specialActor[i][1];
        objText = specialActor[i][0];
        ListObject workflowUserInfo = new ListObject();
        workflowUserInfo.setID(objID);
        workflowUserInfo.setName(objText);
        listSpecialModel.addElement(workflowUserInfo);
      }
      jListSpecialActor.setModel(listSpecialModel);
      initData();
    }catch(Exception e){
      e.printStackTrace();
    }
  }
  
  void initData(){
    String wfProgressIDs=workflowRecordInfoWindow.interruptSubmitter;
    if(Utility.isEmpty(wfProgressIDs)){
      return;
    }
    String[] progressID = WindowUtilities.splitString(wfProgressIDs,";");
    List userIDList= new ArrayList();
    List groupIDList= new ArrayList();
    List roleIDList= new ArrayList();
    List specialActorIDList= new ArrayList();
    
    for(int i=0;i<progressID.length;i++){
      String[] progressItem = WindowUtilities.splitString(progressID[i],"_");
      if(MtmWorkflowStepUserActor.ACTOR_TYPE_USER.equals(progressItem[0])){
        userIDList.add(progressItem[1]);
      } else if(MtmWorkflowStepUserActor.ACTOR_TYPE_GROUP.equals(progressItem[0])) {
        groupIDList.add(progressItem[1]);
      } else if(MtmWorkflowStepUserActor.ACTOR_TYPE_ROLE.equals(progressItem[0])) {
        roleIDList.add(progressItem[1]);
      } else {
        specialActorIDList.add(progressItem[0]);
      }
      
      //WindowUtilities.setListItemByIDs();
    }
    String[] userID=new String[userIDList.size()];
    for(int i=0;i<userIDList.size();i++){
      userID[i]=userIDList.get(i).toString();
    }
    String[] groupID=new String[groupIDList.size()];
    for(int i=0;i<groupIDList.size();i++){
      groupID[i]=groupIDList.get(i).toString();
    }
    String[] roleID=new String[roleIDList.size()];
    for(int i=0;i<roleIDList.size();i++){
      roleID[i]=roleIDList.get(i).toString();
    }
    String[] specialActor=new String[specialActorIDList.size()];
    for(int i=0;i<specialActorIDList.size();i++){
      specialActor[i]=specialActorIDList.get(i).toString();
    }
    WindowUtilities.setListItemByIDs(userList,userID);
    WindowUtilities.setListItemByIDs(groupList,groupID);
    WindowUtilities.setListItemByIDs(roleList,roleID);
    WindowUtilities.setListItemByIDs(jListSpecialActor,specialActor);
    
  }

  void jButton1_actionPerformed(ActionEvent e) {
    String wfProgressIDs = "";
    String progressID = "";
    Object[] listObject=userList.getSelectedValues();    
    for(int i=0;i<listObject.length;i++){
      progressID=MtmWorkflowStepUserActor.ACTOR_TYPE_USER+"_"+((ListObject)listObject[i]).getID();
      if(!Utility.isEmpty(wfProgressIDs)) {
        wfProgressIDs += ";";
      }
      wfProgressIDs += progressID;
    }
    
    listObject=groupList.getSelectedValues();    
    for(int i=0;i<listObject.length;i++){
      progressID=MtmWorkflowStepUserActor.ACTOR_TYPE_GROUP+"_"+((ListObject)listObject[i]).getID();
      if(!Utility.isEmpty(wfProgressIDs)) {
        wfProgressIDs += ";";
      }
      wfProgressIDs += progressID;
    }
    
    listObject=roleList.getSelectedValues();    
    for(int i=0;i<listObject.length;i++){
      progressID=MtmWorkflowStepUserActor.ACTOR_TYPE_ROLE+"_"+((ListObject)listObject[i]).getID();
      if(!Utility.isEmpty(wfProgressIDs)) {
        wfProgressIDs += ";";
      }
      wfProgressIDs += progressID;
    }
    
    listObject=jListSpecialActor.getSelectedValues();    
    for(int i=0;i<listObject.length;i++){
      progressID=((ListObject)listObject[i]).getID()+"_0";
      if(!Utility.isEmpty(wfProgressIDs)) {
        wfProgressIDs += ";";
      }
      wfProgressIDs += progressID;
    }
    if(Utility.isEmpty(wfProgressIDs)){
      workflowRecordInfoWindow.workflowEditor.getEditor().outputMessage(AppletResource.lableArr[179],false);
      return;
    }
    workflowRecordInfoWindow.interruptSubmitter=wfProgressIDs;
    this.dispose();
  }





}

class InterruptOwner_jButton1_actionAdapter implements java.awt.event.ActionListener {
  InterruptOwner adaptee;

  InterruptOwner_jButton1_actionAdapter(InterruptOwner adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}





